package com.wowoai.scheduled;

import com.wowoai.common.CommonConstant;
import com.wowoai.user.entity.UserEntity;
import com.wowoai.user.enums.YesOrNoEnum;
import com.wowoai.user.repository.UserRepositoryI;
import com.wowoai.user.service.UserServiceI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class UserScoreTask {

    @Autowired
    private UserRepositoryI  userRepositoryI;

    /**
     * 每天凌晨初始化用户分数
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void userScoreTask() {
        log.debug("每天十二点刷新用户分数");
        List<UserEntity> userEntities = userRepositoryI.findAll();
        for (UserEntity userEntity : userEntities) {
            userEntity.setAdCount(0);
            if (userEntity.getRole().contains("1")){
                userRepositoryI.update(userEntity);
                continue;
            }
            userEntity.setScore(CommonConstant.defaultScore);
            userEntity.setScoreTagFlag(Boolean.TRUE);
        }
        userRepositoryI.updateAll(userEntities);
    }
}
